<link rel="stylesheet" href="../static/layui/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
<!--<script>-->
<!--    function download_data() {-->
<!--        layui.use(['table'], function () {-->
<!--            layui.table.on('row(currentTableFilter)', function (obj) {-->
<!--                var index =  $(obj.tr).attr("data-index");-->
<!--                data = layui.table.cache["currentTableId"];-->
<!--                console.log(data);-->
<!--                var filename = data[index]["filename"];-->
<!--                console.log(filename);-->
<!--                var file_date = data[index]["upload_date"];-->
<!--                console.log(file_date);-->
<!--                window.location.href = "/api/file?type=2&filename="+filename+"&date="+file_date-->
<!--            });-->

<!--        });-->
<!--    }-->
<!--</script>-->
<style>
    .layui-table-col-special .layui-table-cell{
        height : auto;
        overflow : visible;
        text-overflow : inherit;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div class="layui-upload">
<!--            <label id="lb"></label><br>-->
<!--            <input type="button" value="选取文件" class="layui-btn layui-btn-normal" onclick="getFile()">-->
<!--            <input style="display: none" type="file" name="fileField" id="fileField" size="28"-->
<!--                   onchange="document.getElementById('textfield').value=this.value"/>-->
<!--            <button type="submit" class="layui-btn" id="submitfile" onclick="uploadFile()" lay-filter="sub-btn">-->
<!--                开始上传-->
<!--            </button>-->
            <button type="button" class="layui-btn layui-btn-normal" id="choose_file">
                选择文件
            </button>
            <button type="button" class="layui-btn layui-btn-normal" id="upload_file">
                开始上传
            </button>
        </div>
    </div>
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <!--除 download_data 外， lay-event中的事件未实现-->
    <script type="text/html" id="currentTableBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="match">匹配</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="config">配置</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="download_data">下载源数据</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="download_result">下载结果</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="view_log">日志</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete_file">删除</a>
    </script>
</div>

<script>
    function getFile(){
	    $("#fileField").click();
    }
    var filename="";
    function clickF() {
        filename=$("#getF").val();
        var filenames=filename.split("\\");
        filename=filenames[filenames.length-1];
        $("#lb").text(filename);
    }
    function uploadFile(){
        if ($("#fileField")[0].files[0]==null || $("#fileField")[0].files[0]===undefined){
        alert("请先选择文件!");
        }
        else{
        var formData = new FormData();//这里需要实例化一个FormData来进行文件上传
		formData.append("file",$("#fileField")[0].files[0]);
		$.ajax({
			type : "post",
			url : "/api/file",
			data : formData,
			processData : false,
			contentType : false,
			success : function(result){
				if (result.success == "false") {
					alert("文件提交失败!");
                    layer.closeAll('dialog');
				}
				else if (result.exception == "true"){
				    alert("服务器异常");
                    layer.closeAll('dialog');
				}
				else if (result.success == "true"){
				    alert("文件上传成功!");
				    layui.table.reload('currentTableId');
			    }
			}
		});
		}
    }

</script>
<script src="../static/layui/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../static/layui/lib/jq-module/zyupload/zyupload-1.0.0.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','miniPage','element','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: '/api/file?type=1',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {field: 'filename', width: 500, title: '文件名', align: "center"},
                {field: 'upload_date', width: 500, title: '上传时间', sort: true, align: "center"},
                {field: 'status', width: 150, title: '状态',  align: "center",templet: function(d){if(d.status==1){return "已匹配"}else{return "未匹配"}}},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [15, 30, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

<!--        window.reloadView = function () {-->
<!--          layui.table.reload('currentTableId');-->
<!--      }-->
<!--      setInterval('reloadView()',30000);-->
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('../static/layui/page/table/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
            <!--监听 download_data 按钮 操作-->
            if (obj.event == 'download_data'){
                <!-- 函数直接复制原下载函数 -->
                var index =  $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var filename = data[index]["filename"];
                var file_date = data[index]["upload_date"];
                window.location.href = "/api/file?type=2&filename="+filename+"&date="+file_date
            }
            <!--监听 config 按钮 操作-->
            if (obj.event === 'config') {  // 监听添加操作
                var CurrentStock;
                var RequireStock;
                var index =  $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var file_date = data[index]["upload_date"];
                var content = miniPage.getHrefContent("../static/layui/page/menu_child/config_page/update_config.html?date="+file_date);
                $.ajax({
                    type : "get",
                    url : "/api/config?date="+file_date,
                    data : false,
                    processData : false,
                    contentType : false,
                    success : function(result){
                        if (result.success == "false") {
                        alert("获取配置失败!");
                        layer.closeAll('dialog');
                        }
                    else if (result.exception == "true"){
                        alert("服务器异常");
                        layer.closeAll('dialog');
                        }
                    else if (result.success == "true"){
                        CurrentStock=result.data["CurrentStockMap"];
                        RequireStock=result.data["RequirementMap"];
                        Export=result.data["ExportColumns"];
                        layer.open({
                            title: '字段配置',
                            type: 2,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: ['35%', '75%'],
                            content: "../static/layui/page/menu_child/config_page/update_config.html?date="+file_date,
                            success: function(layero,index){
                                contents =$(layero).find("iframe").contents();
                                contents.find("#unique_code_stock").val(CurrentStock["unique_code"]);
                                contents.find("#item_id_stock").val(CurrentStock["item_id"]);
                                contents.find("#item_size_stock").val(CurrentStock["item_size"]);
                                contents.find("#order_id_stock").val(CurrentStock["order_id"]);
                                contents.find("#item_condition_stock").val(CurrentStock["item_condition"]);
                                contents.find("#order_status_stock").val(CurrentStock["order_status"]);
                                contents.find("#unique_code_require").val(RequireStock["unique_code"]);
                                contents.find("#item_id_require").val(RequireStock["item_id"]);
                                contents.find("#item_size_require").val(RequireStock["item_size"]);
                                contents.find("#item_name_require").val(RequireStock["item_name"]);
                                contents.find("#order_id_require").val(RequireStock["order_id"]);
                                contents.find("#item_condition_require").val(RequireStock["item_condition"]);
                                contents.find("#order_status_require").val(RequireStock["order_status"]);
                                contents.find("#willing_require").val(RequireStock["willing"]);
                                contents.find("#supplier_require").val(RequireStock["supplier"]);
                                for (var i=0;i<Export.length;i++)
                                    {
                                        contents.find("#column_"+(i+1).toString()).val(Export[i]);
                                    }
                            },
                            end: function(){
                                layui.table.reload('currentTableId');
                            }
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                        }
                    }
                });
            }
            <!--监听 view_log 按钮 操作-->
            if (obj.event == 'view_log'){
                var index =  $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var filename = data[index]["filename"];
                var file_date = data[index]["upload_date"];
                if (data[index]["status"]==1){
                    window.location.href = "/api/file?type=6&filename="+filename+"&date="+file_date
                }
                else{
                layer.msg("未匹配结果");
                }
            }
            <!--监听 match 按钮 操作-->
            if (obj.event == 'match'){
                var index =  $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var filename = data[index]["filename"];
                var file_date = data[index]["upload_date"];
                $.ajax({
                    type : "get",
                    url : "/api/file?type=3&filename="+filename+"&date="+file_date,
                    data : false,
                    processData : false,
                    contentType : false,
                    success : function(result){
				        if (result.success == "false") {
					    alert("匹配失败!");
                        layer.closeAll('dialog');
				        }
				    else if (result.exception == "true"){
				        alert("服务器异常");
                        layer.closeAll('dialog');
				        }
                    else if (result.success == "true"){
                        layer.msg("已上传匹配请求");
                        layui.table.reload('currentTableId');
			            }
			        }
                });
            }
            <!--监听 download_result 按钮 操作-->
            if (obj.event == 'download_result'){
                var index =  $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var filename = data[index]["filename"];
                var file_date = data[index]["upload_date"];
                if (data[index]["status"]==1){
                    window.location.href = "/api/file?type=5&filename="+filename+"&date="+file_date
                }
                else{
                layer.msg("未匹配结果");
                }
            }
            <!--监听 delete 按钮 操作-->
            if (obj.event == 'delete_file'){
                layer.confirm('确认删除？(包括结果以及日志)',{
                    title:'确认',
                    btn:['确认','取消']
                },function(){
                    var index =  $(obj.tr).attr("data-index");
                    data = layui.table.cache["currentTableId"];
                    var filename = data[index]["filename"];
                    var file_date = data[index]["upload_date"];
                    var Object = {"filename":filename,"date":file_date}
                    $.ajax({
                        type : "get",
                        url : "/api/file?type=4&filename="+filename+"&date="+file_date,
                        data : false,
                        processData : false,
                        contentType : false,
                    success : function(result){
                        if (result.success == "false") {
                            alert("删除失败！");
                            layer.closeAll('dialog');
                        }
                        else if (result.exception == "true"){
                            alert("服务器异常");
                            layer.closeAll('dialog');
                        }
                        else if (result.success == "true"){
                            layer.msg("已删除文件！");
                            layui.table.reload('currentTableId');
                        }
                    }
                })
		});
            }
        });

        <!--上传文件-->
        var upload = layui.upload;
        var $ = layui.$;
        upload.render({
            elem: '#choose_file' //绑定元素
            ,url: '/api/file' //上传接口
            ,method : 'POST'
            ,auto : false
            ,drag : true
            <!--前端设置文件类型为 xls、xlsx-->
            ,accept : 'file'
            ,acceptMime:'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ,exts : 'xls|xlsx'
            ,multiple : true
            ,bindAction : '#upload_file'
            ,done: function(res){
                //上传完毕回调
                if(res.success == "true"){
                    alert("上传成功");
                    table.reload('currentTableId');
                }
                if(res.success == "false"){
                    alert("上传失败！");
                    layer.closeAll('dialog');
                }
                if(res.exception == "true"){
                    alert("上传接口异常，请联系管理员");
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });

    });

</script>